﻿using CRM.Database.Model;

namespace CRM.Syncroniser
{
 public   class CoursesSyncroniser:ISyncronise
    {


     /// <summary>
     /// Учебные курсы из CDB
     /// </summary>
     public DbInterface.LearningClientsPlanCourses CDBCourses
     {
        get { return DbInterface.DataLayer.LearningClientsPlanCourses; }
     }


     /// <summary>
     /// Учебные курсы из CRM
     /// </summary>
     public Database.Model.TrainingsCoursesCollection CRMCourses
     {
         get { return new TrainingsCoursesCollection(); }
     }

     #region Implementation of ISyncronise

     /// <summary>
     /// Запуск синхронизации
     /// </summary>
     /// <returns>Результат запуска</returns>
     public bool Syncronise()
     {
         foreach(var item in CDBCourses)
         {
             if(CRMCourses.GetItemByName(item.Name)==null)
             {
                 var course = new TrainingsCourses
                                  {
                                      Name = item.Name,
                                      IsActive = true,
                                      ParticipantCount = item.MaxMembersCount,
                                      Duration = item.Length
                                  };

                 var ctCol = new RubricsCollection();
                 var idCourseType = ctCol.GetIdByName(item.LearningCoursesRubric.Name);
                 if (idCourseType > 0)
                     course.IdRubric = idCourseType;
                



              if(!CRMCourses.Add(course, "Max"))
                  return false;
             }
         }

         return true;
     }

     #endregion
    }
}
